<?php

namespace app\index\controller;


use app\index\model\Articles;
use app\index\model\Users;
use think\Controller;
use think\Exception\DbException;
use think\Request;
use think\Validate;


class Article extends Controller
{
//    获取文章数据
    public function index()
    {
        $articles = new Articles();
        try {
            $articles = $articles->paginate(10); //使用分页读取数据
        } catch (DbException $e) {
            $this->error($e->getMessage());
        }

        $this->assign('articles', $articles);
        return $this->fetch();
    }

    /**
     * 添加文章
     *
     * @param Request $request
     *
     * @return mixed
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(Request $request)
    {
        if ($request->isPost()) {
            $this->addPost($request);
        } else {
            if (session('username')) {
                return $this->fetch();
            } else {
                $this->error('请先登录', '/login');
            }
        }
    }

    /**
     * 编辑数据
     *
     * @param Request $request
     *
     * @throws DbException
     */
    public function edit(Request $request)
    {
        $id = $request->param('id'); //获取文章id
        $post = (new Articles)->get($id);//获取文章

        if ($request->isPost()) {
            $this->editPost($request, $id);
        } else {
            $this->assign('post', $post);
            return $this->fetch();
        }
    }

    /**
     * 删除文章
     * @param Request $request
     */
    public function delete(Request $request){
        $id=$request->param('id');
        Articles::destroy($id);
        $this->success('文章删除成功','/article');
    }

    /**
     * @param Request $request
     *
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function addPost(Request $request): void
    {
        $post = $request->param();
        $validate = Validate::make([
            'title' => 'require',
            'content' => 'require',
        ]);
        $status = $validate->check($post);
        if ($status) {
            $post = $request->param();
            $user = Users::find(session('id'));
            $article = $user->article()->save($post);
            if ($article) {
                $this->success('文章添加成功', '/article');
            } else {
                $this->error('文章添加失败');
            }
        } else {
            $this->error('标题和内容为必填项');
        }
    }

    /**
     * @param Request $request
     * @param $id
     */
    private function editPost(Request $request, $id): void
    {
        $newPost = (new Articles)->where('id', $id)
            ->update([
                'title' => $request->param('title'),
                'content' => $request->param('content'),
                'update_time' => time(),
            ]);

        if ($newPost) {
            $this->success('修改成功', '/article');
        } else {
            $this->error('修改失败');
        }
    }
}
